package org.jsbd.boss.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.dao.IScriptDAO;
import org.jsbd.boss.domian.channel.ChannelScriptItem;
import org.jsbd.boss.domian.channel.ChannelScriptRepo;
import org.jsbd.boss.domian.channel.ChannelScriptRepoMark;
import org.springframework.stereotype.Repository;

@Repository("scriptDao")
public class ScriptDAOImpl extends CrackBaseDao implements IScriptDAO {

	@Override
	public Map<String, Object> selectScriptRepoListJson(DataGridModel page, ChannelScriptRepo channelScriptRepo) {
		channelScriptRepo = (channelScriptRepo == null ? new ChannelScriptRepo() : channelScriptRepo);
		channelScriptRepo.setPageInfo(page);
	    Map<String, Object> results = new HashMap<String, Object>();
	    results.put("rows", super.queryForList("qy_script.SELECT_SCRIPT_REPO_LIST", channelScriptRepo));
	    results.put("total", super.queryForCount("qy_script.SELECT_SCRIPT_REPO_COUNT", channelScriptRepo));
	    return results;
	}

	@Override
	public void insertChannelScriptRepo(ChannelScriptRepo channelScriptRepo) {
		super.insert("qy_script.INSERT_CHANNEL_SCRIPT_REPO", channelScriptRepo);
	}

	@Override
	public ChannelScriptRepo selectScriptRepoByName(String name) {
		return (ChannelScriptRepo) super.queryForObject("qy_script.SELECT_SCRIPT_REPO_BY_NAME", name);
	}

	@Override
	public ChannelScriptRepo selectScriptRepoById(Long id) {
		return (ChannelScriptRepo) super.queryForObject("qy_script.SELECT_SCRIPT_REPO_BY_ID", id);
	}

	@Override
	public void updateChannelScriptRepo(ChannelScriptRepo channelScriptRepo) {
		super.update("qy_script.UPDATE_CHANNEL_SCRIPT_REPO", channelScriptRepo);
	}

	@Override
	public List<ChannelScriptRepoMark> selectMarksByRepoId(Long id) {
		return super.queryForList("qy_script.SELECT_MARKS_BY_REPOID", id);
	}

	@Override
	public void deleteMarksByRepoId(Long repoId) {
		super.delete("qy_script.DELETE_MARKS_BY_REPOID", repoId);
	}

	@Override
	public void insertMark(List<ChannelScriptRepoMark> list) {
		super.batchInsert("qy_script.INSERT_MARK", list);
	}

	@Override
	public Map<String, Object> selectScriptListJson(DataGridModel page, ChannelScriptItem channelScriptItem) {
		channelScriptItem = (channelScriptItem == null ? new ChannelScriptItem() : channelScriptItem);
		channelScriptItem.setPageInfo(page);
	    Map<String, Object> results = new HashMap<String, Object>();
	    results.put("rows", super.queryForList("qy_script.SELECT_SCRIPT_LIST", channelScriptItem));
	    results.put("total", super.queryForCount("qy_script.SELECT_SCRIPT_COUNT", channelScriptItem));
	    return results;
	}

	@Override
	public List<ChannelScriptRepo> selectScriptRepoByEnable(Integer isEnable) {
		return super.queryForList("qy_script.SELECT_SCRIPT_REPO_BY_ENABLE", isEnable);
	}

	@Override
	public void insertChannelScriptItem(List<ChannelScriptItem> list) {
		super.batchInsert("qy_script.INSERT_CHANNEL_SCRIPT_ITEM", list);
	}

	@Override
	public void abandonScript(String name) {
		super.update("qy_script.ABANDON_SCRIPT", name);
	}

	@Override
	public ChannelScriptRepoMark selectScriptRepo(String mark) {
		return (ChannelScriptRepoMark) super.queryForObject("qy_script.SELECT_SCRIPT_REPO", mark);
	}

	@Override
	public List<String> selectFilePathByRepoIdAndType(Long repoId, Integer type) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("repoId", repoId);
		params.put("type", type);
		return super.queryForList("qy_script.SELECT_FILEPATH_BY_REPOID", params);
	}

	@Override
	public ChannelScriptItem selectScriptItemByName(String name) {
		return (ChannelScriptItem) super.queryForObject("qy_script.SELECT_SCRIPT_ITEM_BY_NAME", name);
	}

	@Override
	public List<ChannelScriptRepoMark> selectAllMarks() {
		return super.queryForList("qy_script.SELECT_ALL_MARKS");
	}

	@Override
	public void deleteByMark(String mark) {
		super.delete("qy_script.DELETE_BY_MARK", mark);
	}

	@Override
	public List<ChannelScriptItem> selectChannelScriptItemByRepoid(Long repoId) {
		return super.queryForList("qy_script.SELECT_CHANNEL_SCRIPT_ITEM_BY_REPOID", repoId);
	}

	@Override
	public void updateConfigScriptNum(ChannelScriptItem channelScriptItem) {
		super.update("qy_script.UPDATE_CONFIG_SCRIPT_NUM", channelScriptItem);
	}

	@Override
	public List<String> selectFilePathByRepoId(Long repoId) {
		return super.queryForList("qy_script.SELECT_FILE_PATH_BY_REPOID", repoId);
	}

}
